.section .data	
	str1:
		.string "请选择>\n"
	str2:
		.string "%s\n"
	error:
		.string "输入错误请重新选择>\n"
.equ SYS_READ,0
.equ STDIN,0
.equ SYS_EXIT,60
.section .rodata
.align 8
	.L7:
		.quad .L0  #退出
		.quad .L1  #增加	
		.quad .L2  #删除
		.quad .L3  #编辑
		.quad .L4  #查找	
		.quad .L5  #打印
		.quad .L6  #输入错误
.section .text
.global _start
_start:
	nop
	mov %rsp,%rbp
	subq $24,%rsp	
#将分配的这48个字节的空间清空
	mov $24,%rcx
	cld
	leaq -24(%rbp),%rdi
	mov $0,%al
	rep stosb
#初始化通讯录内存
	leaq -16(%rbp),%rdi
	call init	
0:
	call menu
#提示用户选择
	mov $str1,%rdi
	mov $0,%rax
	call printf
	mov $0,%rax
#获取用户输入写入数据到input
	mov $SYS_READ,%rax
	mov $STDIN,%rdi
	leaq -20(%rbp),%rsi
	mov $4,%rdx  #写入四个字节
	syscall
	test %rax,%rax
	js 1f
	
#switch,case流程
	leaq -20(%rbp),%rsi #input
#调用atoi函数将字符串转为数字
	mov %rsi,%rdi
	call atoi
	mov %rax,%rsi	
	cmpq $0,%rsi
	jb .L6
	cmpq $5,%rsi
	ja .L6
	jmp *.L7(,%rsi,8)

#退出
.L0:
	leaq -16(%rbp),%rdi
	call SaveContact
	leaq -16(%rbp),%rdi
	call DestroyContact
	jmp 1f
	
#添加
.L1:
	leaq -16(%rbp),%rdi
	call AddContact
	jmp 0b
#删除
.L2:
	leaq -16(%rbp),%rdi
	call DelContact
	jmp 0b
#修改
.L3:
	leaq -16(%rbp),%rdi
	call ModifyContact
	jmp 0b
#查找
.L4:
	leaq -16(%rbp),%rdi
	call SearchContact
	jmp 0b
#打印
.L5:
	leaq -16(%rbp),%rdi
	call PrintContact
	jmp 0b
#输入错误	
.L6:
	mov $0,%rax
    mov $error,%rdi
    call printf
    mov $0,%rax
	jmp 0b

1:
	neg %rax
	mov %rax,%rdi
	call strerror
#根据错误状态码打印错误信息
	mov %rax,%rsi
	mov $str2,%rdi
	mov $0,%rax
	call printf

	mov $0,%rdi
	mov $SYS_EXIT,%rax
	syscall
